package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.qc.QcResultConvertRule;
import com.ysd.lis.mapper.qc.QcResultConvertRuleMapper;
import com.ysd.lis.request.qc.QcResultConvertRuleDto;
import com.ysd.lis.request.qc.QcResultConvertRuleParam;
import com.ysd.lis.service.qc.QcResultConvertRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-25
 */
@Service
@Slf4j
public class QcResultConvertRuleServiceImpl extends ServiceImpl<QcResultConvertRuleMapper, QcResultConvertRule> implements QcResultConvertRuleService {

    @Autowired
    QcResultConvertRuleMapper qcResultConvertRuleMapper;
    @Override
    public Result findConvertRuleList(QcResultConvertRuleParam param) {
        MPJLambdaWrapper<QcResultConvertRule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QcResultConvertRule.class)
                .selectAs("lid",LabItemDict::getItemNa,QcResultConvertRule::getItemDictName)
                .selectAs("lid",LabItemDict::getItemEnCode,QcResultConvertRule::getItemEnCode)
                .leftJoin(LabItemDict.class,"lid",LabItemDict::getId,QcResultConvertRule::getItemDictId);
        if (StringUtils.isNotBlank(param.getInstrId())) {
            queryWrapper.eq(QcResultConvertRule::getInstrId,param.getInstrId());
        }
        queryWrapper.eq(QcResultConvertRule::getDelFlag, 0)
                .orderByAsc(QcResultConvertRule::getSeq);
        List<QcResultConvertRule> qcResultConvertRules = qcResultConvertRuleMapper.selectList(queryWrapper);
        List<QcResultConvertRule> all = qcResultConvertRules.stream().filter(r -> r.getItemDictId().equals("all")).collect(Collectors.toList());
        for(QcResultConvertRule rule :all){
            rule.setItemEnCode("all");
            rule.setItemDictName("所有项目");
        }

        return Result.succ(1, "查询成功", qcResultConvertRules);
    }

    @Override
    public Result saveOrUpdateConvertRule(QcResultConvertRuleDto dto) {
        List<QcResultConvertRule> qcResultConvertRuleList = dto.getQcResultConvertRuleList();
        boolean b = this.saveOrUpdateBatch(qcResultConvertRuleList);
        if(b){
            return Result.succ(1, "成功", null);
        }else {
            return Result.fail(400,"失败","");
        }
    }
    @Override
    public Result batchDeleteConvertRuleList(QcResultConvertRuleDto dto) {
        List<QcResultConvertRule> qcResultConvertRuleList = dto.getQcResultConvertRuleList();
        List<String> ids = qcResultConvertRuleList.stream().map(QcResultConvertRule::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
